package com.tommy.ddp.manager
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.grid.DdpGrid;

	/**
	 * 消除后填充空格的管理
	 * @author hongxuan
	 * 
	 */	
	public class FillUpManager
	{
		public function FillUpManager()
		{
		}
		
		public static function fillUp(vect:Vector.<int>):void
		{
			for each(var i:int in vect)
			{
				computer(i);
			}
		}
		/**
		 * 计算每一列需要补充多少个
		 * @param v 列
		 * 
		 */		
		private static function computer(v:int):void
		{
			var avaliableGridNum:uint;
			var avaliableGridIndexList:Vector.<int> = new Vector.<int>();
			var boxNum:uint;
			//当前列所有的BOX集合
			var allColumnBoxList:Vector.<DdpBox> = new Vector.<DdpBox>();
			
			for(var i:uint = 0;i<9;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(v,i);
				if(grid.isAvaliable)
				{
					avaliableGridNum++;
					avaliableGridIndexList.push(i);
				}
			}
			
			for(i = 0;i<9;i++)
			{
				var box:DdpBox = GridDataManager.getBox(v,i);
				if(box)
				{
					allColumnBoxList.push(box);
					boxNum++;
				}
			}
			
			var missingNum:int = avaliableGridNum - boxNum;
			for(i = 0;i<missingNum;i++)
			{
				var newBox:DdpBox = new DdpBox();
				newBox.column = v;
				newBox.row = -i-1;
				newBox.type = Math.floor(Math.random()*3);
				
				Game.instance.container.addChild(newBox);
				
				allColumnBoxList.unshift(newBox);
			}
			//
			var count:uint = 0;
			for each(var j:DdpBox in allColumnBoxList)
			{
				j.moveRow(avaliableGridIndexList[count]);
				count++;
			}
		}
	}
}